package com.bndg.browser.util

import android.webkit.WebView
import com.bndg.browser.ActivityManager
import com.bndg.browser.app.MyApplication
import java.io.InputStream
import java.nio.charset.StandardCharsets


object OtherUtils {
    fun loadJavaScriptFromAssets(fileName: String): String {
        var result = ""
        try {
            val inputStream: InputStream =
                ActivityManager.getInstance().getApplication().assets.open(fileName)
            val size = inputStream.available()
            val buffer = ByteArray(size)
            inputStream.read(buffer)
            inputStream.close()
            result = String(buffer, StandardCharsets.UTF_8)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return result
    }

    fun injectJavaScript(view: WebView, jsFile: String) {
        val jsCode: String = loadJavaScriptFromAssets(jsFile)
        view.evaluateJavascript(jsCode, null)
    }

    var clickTimestamp: Long = 0
    fun isDoubleClick(): Boolean {
        val currentTimestamp = System.currentTimeMillis()
        if (currentTimestamp - clickTimestamp < 500) {
            return true
        } else {
            clickTimestamp = currentTimestamp
            return false
        }
    }
}
